package com.haste.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.haste.common.domain.po.BaseDictPo;
import com.haste.common.service.IBaseDictService;
import com.haste.system.domain.vo.TreeNode;
import com.haste.system.service.IDictService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author haste
 */
@Service
public class DictServiceImpl implements IDictService {
    @Resource
    private IBaseDictService baseDictService;
    @Resource
    private CommonService commonService;
    @Override
    public List<TreeNode> tree() {
        List<BaseDictPo> list = baseDictService.list();
        if (CollUtil.isEmpty(list)){
            return null;
        }
        List<TreeNode> treeNodes = list.stream().map(baseDictPo -> {
            TreeNode treeNode = new TreeNode();
            treeNode.setId(baseDictPo.getId());
            treeNode.setName(baseDictPo.getName());
            treeNode.setType(baseDictPo.getCode());
            treeNode.setShortName(baseDictPo.getCode());
//            treeNode.setNodeType("dict");
            treeNode.setSort(baseDictPo.getSort());
            if (StrUtil.isNotBlank(baseDictPo.getAncestor())){
                String[] split = baseDictPo.getAncestor().split("/");
                treeNode.setParentId(split[split.length-1]);
            }
            return treeNode;
        }).collect(Collectors.toList());
        return commonService.list2Tree(treeNodes);
    }

    @Override
    public void save(BaseDictPo baseDictPo) {
        LambdaQueryWrapper<BaseDictPo> wrapper = new LambdaQueryWrapper<>();
        wrapper.isNull(BaseDictPo::getAncestor);
        wrapper.or().eq(BaseDictPo::getAncestor,"");
        List<BaseDictPo> list = baseDictService.list(wrapper);
        // 设置code
        String ancestor = baseDictPo.getAncestor();
        if (CollUtil.isNotEmpty(list)){
            if (StrUtil.isBlank(ancestor)){
                list.stream().map(it -> {
                    String code = it.getCode();
                    long longCode = 0L;
                    if (StrUtil.isNotBlank(code)){
                        longCode = Long.valueOf(code);
                    }
                    return longCode;
                }).max(Long::compareTo).ifPresent(it -> {
                    baseDictPo.setCode(String.valueOf(it+100));
                });
            }
            else{
                wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(BaseDictPo::getAncestor,ancestor);
                wrapper.or().eq(BaseDictPo::getId,ancestor);
                wrapper.orderByDesc(BaseDictPo::getCode);
                list = baseDictService.list(wrapper);
                if (CollUtil.isNotEmpty(list)){
                    BaseDictPo temp = list.get(0);
                    baseDictPo.setCode(String.valueOf(Long.valueOf(temp.getCode())+1));
                }
            }
        }

        // 设置ancestor
        wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaseDictPo::getId,ancestor);
        list = baseDictService.list(wrapper);
        if (CollUtil.isNotEmpty(list)){
            BaseDictPo temp = list.get(0);
            String tempAncestor = temp.getAncestor();
            if (StrUtil.isNotBlank(tempAncestor)){
                ancestor = tempAncestor+"/"+temp.getId();
            }
            baseDictPo.setAncestor(ancestor);
        }
        baseDictService.save(baseDictPo);
    }
}
